package com.zhonghan.recharge.service.impl;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.zhonghan.common.bean.Result;
import com.zhonghan.common.variable.GlobalVariable;
import com.zhonghan.recharge.dao.ActivityDao;
import com.zhonghan.recharge.pojo.Activity;
import com.zhonghan.recharge.service.ActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class ActivityServiceImpl implements ActivityService{
    @Autowired
    private ActivityDao activityDao;
    @Override
    public long getActivityRowCount(){
        return activityDao.getActivityRowCount();
    }
    @Override
    public List<Activity> selectActivity(){
        return activityDao.selectActivity();
    }
    @Override
    public Activity selectActivityByObj(Activity obj){
        return activityDao.selectActivityByObj(obj);
    }
    @Override
    public Activity selectActivityById(Integer id){
        return activityDao.selectActivityById(id);
    }
    @Override
    public int insertActivity(Activity value){
        return activityDao.insertActivity(value);
    }
    @Override
    public int insertNonEmptyActivity(Activity value){
        return activityDao.insertNonEmptyActivity(value);
    }
    @Override
    public int insertActivityByBatch(List<Activity> value){
        return activityDao.insertActivityByBatch(value);
    }
    @Override
    public int deleteActivityById(Integer id){
        return activityDao.deleteActivityById(id);
    }
    @Override
    public int updateActivityById(Activity enti){
        return activityDao.updateActivityById(enti);
    }
    @Override
    public int updateNonEmptyActivityById(Activity enti){
        return activityDao.updateNonEmptyActivityById(enti);
    }

    public ActivityDao getActivityDao() {
        return this.activityDao;
    }

    public void setActivityDao(ActivityDao activityDao) {
        this.activityDao = activityDao;
    }
	@Override
	public Result saveActivity(String activityName, String activityUrl, String beginTime, String endTime,
			String activityDescription) {
		
		Result result = new Result();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			Activity value = new Activity();
			value.setActivityName(activityName);
			value.setActivityDescription(activityDescription);
			value.setActivityUrl(activityUrl);
			value.setBeginTime(sdf.parse(beginTime));
			value.setEndTime(sdf.parse(endTime));
			value.setCreateTime(new Date());
			
			int rs = activityDao.insertNonEmptyActivity(value );
			
			GlobalVariable.activity = value;
			
			if(rs > 0){
				result.setSuccess("1", null, "添加成功!");
			}else{
				result.setFailure("0", "添加失败!");
			}
			
		} catch (Exception e) {
			result.setFailure("-1", "系统繁忙,请稍后再试!");
			e.printStackTrace();
		}
		
		return result;
	}

}